#!/usr/bin/env python
#
#   Copyright (C) 2006 by Patrick Stinson and Jonathan Saggau             
#   patrickkidd@gmail.com   saggau@gmail.com                              
#                                                                         
#   This program is free software; you can redistribute it and/or modify  
#   it under the terms of the GNU General Public License as published by  
#   the Free Software Foundation; either version 2 of the License, or     
#   (at your option) any later version.                                   
#                                                                         
#   This program is distributed in the hope that it will be useful,       
#   but WITHOUT ANY WARRANTY; without even the implied warranty of        
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         
#   GNU General Public License for more details.                          
#                                                                         
#   You should have received a copy of the GNU General Public License     
#   along with this program; if not, write to the                         
#   Free Software Foundation, Inc.,                                       
#   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.  
#
"""
High level Server class, like the one in sclang.
"""

import os
import scosc
import process
import msg

def quit():
    return msg.msg("/quit")
    
def notify(argBool):
    return msg.msg('/notify', argBool)
                             
def status():
    return msg.msg('/status')
    
def cmd(*args): 
    return msg.msg("/cmd", args)
    
def dumpOSC(codeInt):
    return msg.msg('/dumpOSC', codeInt)
    
def sync(replyInt = None):
    return msg.msg('/sync', replyInt)

class _Server(scosc.Controller):
    """ Strictly for convenince. This is poorly thought-out and so
    probably bad design.
    """
    
    proc = None

    def __init__(self, addr, verbose=False):
        scosc.Controller.__init__(self, addr, verbose)
        
    def quit(self):
        self.sendMsg('/quit')
        self.receive('/done', '/fail')

    def kill(self):
        if self.proc:
            self.proc.kill()

    def ensure_dead(self):
        return
        if self.proc.isAlive():
            self.kill()
    
class Server(_Server):
    def __init__(self, addr, verbose=False):
        _Server.__init__(self, addr, verbose)
        
    # the rest goes here as we need it

def connect(ip, port):
    s = _Server((ip, port))
    s.sendMsg('/status', 1)
    s.receive('status.reply')
    return s

def start(exe='scsynth', exedir=os.getcwd(),
          inputs=2, outputs=2, samplerate=48000):
    instance = process.start_local(exe, exedir, 57110,
                                   inputs, outputs, samplerate)
    s = connect('127.0.0.1', 57110)
    s.instance = instance
    return s

def test_start():
    import time
    s = start('scsynth', os.getcwd(), 2, 2, 48000)
    time.sleep(1)
    s.quit()

def test_connect():
    s = connect('127.0.0.1', 57110)
    s.quit()

if __name__ == '__main__':
    test_start()
    #test_connect()
